<template>
  <div class="page-box">
    <el-card class="page-box-card">
      <el-form
        :model="queryParams"
        ref="queryForm"
        size="small"
        label-width="88px"
        inline
      >
        <el-form-item label="接口名称" prop="param.name">
          <el-input
            v-model="queryParams.param.name"
            placeholder="请输入接口名称"
            clearable
            style="width: 240px"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="请求CODE" prop="param.code">
          <el-input
            v-model="queryParams.param.code"
            placeholder="请输入请求CODE"
            clearable
            style="width: 240px"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="接口URI" prop="param.uri">
          <el-input
            v-model="queryParams.param.uri"
            placeholder="请输入接口URI"
            clearable
            style="width: 240px"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="接口分类" prop="param.categoryIds">
          <el-cascader
            style="width: 240px"
            v-model="queryParams.param.categoryIds"
            :props="props"
            :show-all-levels="false"
            :options="treeData"
            clearable
          >
          </el-cascader>
          <!-- <el-input
            v-model="queryParams.param.name"
            placeholder="请输入接口分类"
            clearable
            style="width: 240px"
            @keyup.enter.native="handleQuery"
          /> -->
        </el-form-item>
        <el-form-item label="请求方法" prop="param.method">
          <el-select
            style="width: 240px"
            v-model="queryParams.param.method"
            placeholder="请选择任务组名"
            clearable
          >
            <el-option
              v-for="dict in dict.type.quality_api_method"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
          <!-- <el-input
            v-model="queryParams.param.name"
            placeholder="请输入请求方法"
            clearable
            style="width: 240px"
            @keyup.enter.native="handleQuery"
          /> -->
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            icon="el-icon-search"
            size="small"
            @click="handleQuery"
            >搜索</el-button
          >
          <el-button icon="el-icon-refresh" size="small" @click="resetQuery"
            >重置</el-button
          >
        </el-form-item>
      </el-form>
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd"
            v-hasPermi="['definition:class:add']"
            >新增</el-button
          >
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="el-icon-edit"
            size="mini"
            :disabled="single"
            @click="handleEdit"
            v-hasPermi="['definition:class:edit']"
            >修改</el-button
          >
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="danger"
            plain
            icon="el-icon-delete"
            size="mini"
            :disabled="multiple"
            @click="handleDelete"
            v-hasPermi="['definition:class:delete']"
            >删除</el-button
          >
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="el-icon-upload2"
            size="mini"
            v-hasPermi="['definition:class:upload']"
            >导入</el-button
          >
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="warning"
            plain
            icon="el-icon-download"
            size="mini"
            v-hasPermi="['definition:class:download']"
            >导出</el-button
          >
        </el-col>
      </el-row>
      <el-table
        v-loading="loading"
        :data="dataList"
        style="width: 100%"
        @selection-change="handleSelectionChange"
      >
        <el-table-column
          type="selection"
          width="55"
          align="center"
          fixed="left"
        />
        <el-table-column prop="sort" label="排序" width="60" fixed="left" />
        <!-- <el-table-column prop="id" label="接口ID" width="180" >
        </el-table-column> -->
        <el-table-column prop="name" label="接口名称" width="180" fixed="left">
        </el-table-column>
        <el-table-column prop="categoryName" label="接口分类" width="180">
        </el-table-column>
        <el-table-column prop="uri" label="接口URI" width="180">
        </el-table-column>
        <el-table-column prop="method" label="请求方法" width="80">
          <template slot-scope="scope">
            <dict-tag
              :options="dict.type.quality_api_method"
              :value="scope.row.method"
            />
          </template>
        </el-table-column>
        <el-table-column prop="code" label="请求CODE" width="180">
        </el-table-column>
        <el-table-column prop="contentType" label="Content-Type" width="120">
        </el-table-column>
        <el-table-column prop="inputParam" label="Body参数" width="180">
        </el-table-column>
        <el-table-column prop="input" label="参数示例"> </el-table-column>
        <el-table-column prop="outputParam" label="输出参数"> </el-table-column>
        <el-table-column prop="output" label="输出示例"> </el-table-column>
        <el-table-column label="操作" align="center" fixed="right" width="140">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleEdit(scope.row)"
              v-hasPermi="['system:dept:edit']"
              >修改</el-button
            >
            <el-button
              size="mini"
              type="text"
              style="color: red"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
              v-hasPermi="['system:dept:edit']"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </el-card>
    <!-- 添加或修改部门对话框 -->
    <el-dialog
      :title="title"
      :visible.sync="addDialog"
      width="888px"
      append-to-body
    >
      <el-form
        ref="addForm"
        :model="addForm"
        :rules="rules"
        label-width="120px"
      >
        <el-row>
          <el-col :span="24">
            <el-form-item label="接口名称" prop="name">
              <el-input
                v-model="addForm.name"
                placeholder="请输入接口名称"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="排序" prop="sort">
              <el-input-number
                style="width: 240px"
                v-model="addForm.sort"
                :min="0"
                label="请输入接口排序"
              ></el-input-number>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="接口URI" prop="uri">
              <el-input
                style="width: 100%"
                v-model="addForm.uri"
                label="请输入接口接口URI"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="请求方法" prop="method">
              <el-select
                style="width: 100%"
                v-model="addForm.method"
                placeholder="请选择接口请求方法"
                clearable
              >
                <el-option
                  v-for="dict in dict.type.quality_api_method"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="请求CODE" prop="code">
              <el-input
                style="width: 100%"
                v-model="addForm.code"
                label="请输入接口请求CODE"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="接口分类" prop="categoryIds">
              <!-- <el-input
                style="width: 100%"
                v-model="addForm.code"
                label="请输入接口接口分类"
              ></el-input> -->
              <el-cascader
                style="width: 240px"
                v-model="addForm.categoryIds"
                :props="props"
                :show-all-levels="false"
                :options="treeData"
              >
              </el-cascader>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="Content-Type" prop="contentType">
              <el-input
                style="width: 100%"
                v-model="addForm.contentType"
                label="请输入接口Content-Type"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="Body参数" prop="inputParam">
              <el-input
                style="width: 100%"
                v-model="addForm.inputParam"
                label="请输入接口Body参数"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="参数示例" prop="input">
              <el-input
                style="width: 100%"
                v-model="addForm.input"
                label="请输入接口参数示例"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="输出参数" prop="outputParam">
              <el-input
                style="width: 100%"
                v-model="addForm.outputParam"
                label="请输入接口输出参数"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="输出示例" prop="output">
              <el-input
                style="width: 100%"
                v-model="addForm.output"
                label="请输入接口输出示例"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import {
  insertInterface,
  updateInterface,
  deleteInterface,
  listInterface,
} from "@/api/collect/interface";
import { treeCategory } from "@/api/collect/classification";
export default {
  dicts: ["quality_api_method"],
  data() {
    return {
      // 遮罩层
      loading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      selectionData: [],
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        param: {
          categoryIds: [],
          categoryId: "",
          code: "",
          method: "",
          name: "",
          uri: "",
        },
      },
      props: {
        checkStrictly: true,
        label: "name",
        value: "id",
      },
      dataList: [],
      total: 0,
      treeData: [],
      title: "",
      addDialog: false,
      addForm: {
        name: "",
      },
      rules: {
        name: [{ required: true, message: "请输入接口名称", trigger: "blur" }],
        sort: [{ required: true, message: "请输入排序", trigger: "blur" }],
        uri: [{ required: true, message: "请输入接口URI", trigger: "blur" }],
        method: [
          { required: true, message: "请选择请求方法", trigger: "blur" },
        ],
        code: [{ required: true, message: "请输入请求CODE", trigger: "blur" }],
        categoryIds: [
          { required: true, message: "请选择接口分类", trigger: "blur" },
        ],
        contentType: [
          { required: true, message: "请输入Content-Type", trigger: "blur" },
        ],
        inputParam: [
          { required: true, message: "请输入Body参数", trigger: "blur" },
        ],
        input: [{ required: true, message: "请输入参数示例", trigger: "blur" }],
        outputParam: [
          { required: true, message: "请输入输出参数", trigger: "blur" },
        ],
        output: [
          { required: true, message: "请输入输出示例", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.getList();
    this.getTreeData();
  },
  methods: {
    getTreeData() {
      treeCategory({}).then((res) => {
        this.treeData = [...res.data];
        // console.log("树状数据: ");
      });
    },
    // 搜索事件
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    // 重置事件
    resetQuery() {
      this.dataList = [];
      this.total = 0;
      this.queryParams.pageNum = 1;
      this.queryParams.pageSize = 10;
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 获取数据列表
    getList() {
      this.loading = true;
      const params = { ...this.queryParams };
      if (params.param.categoryIds && params.param.categoryIds.length) {
        const length = params.param.categoryIds.length;
        params.param.categoryId = params.param.categoryIds[length - 1];
      } else {
        params.param.categoryId = null;
      }
      listInterface(params).then((response) => {
        this.dataList = response.data.list;
        this.total = response.data.total;
        this.loading = false;
      });
      this.loading = false;
    },
    // 列表选中事件
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.selectionData = [...selection];
      this.single = selection.length != 1;
      this.multiple = !selection.length;
    },
    // 新增接口
    handleAdd() {
      this.title = "新增接口";
      this.addDialog = true;
    },
    // 新增/修改弹框提交事件
    submitForm() {
      this.$refs["addForm"].validate((valid) => {
        if (valid) {
          const param = { ...this.addForm };
          param.categoryId =
            param.categoryIds && param.categoryIds.length
              ? param.categoryIds[param.categoryIds.length - 1]
              : "";
          param.status = "0";
          if (param.id) {
            // 修改
            updateInterface(param).then((res) => {
              this.$message.success("修改成功");
              this.cancel();
              this.handleQuery();
            });
          } else {
            // 新增
            insertInterface(param).then((res) => {
              this.$message.success("新增成功");
              this.cancel();
              this.handleQuery();
            });
          }
        } else {
          return false;
        }
      });
    },
    // 新增/修改弹框取消事件
    cancel() {
      this.addForm = {
        name: "",
      };
      this.addDialog = false;
    },
    // 修改接口
    handleEdit(row) {
      this.title = "修改接口";
      const data = row.id ? row : this.selectionData[0];
      const categoryIds = [...data.ancestors, data.categoryId];
      categoryIds.shift();
      this.addForm = { ...data };
      this.addForm.categoryIds = [...categoryIds];
      this.addDialog = true;
    },
    // 删除接口
    handleDelete(row) {
      const idList = row.id ? [row.id] : [...this.ids];
      this.$modal
        .confirm("是否确认删除当前选择的接口？")
        .then(function () {
          // 删除
          return deleteInterface({ idList });
        })
        .then(() => {
          this.$message.success("删除成功");
          this.handleQuery();
        })
        .catch(() => {});
    },
  },
};
</script>
